﻿using Exam.infranstuctrue.Dto.Request.Error;
using Exam.infranstuctrue.Dto.Respones;
using Exam.infranstuctrue.Tools.Attributes;
using Exam.Service.Interface;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Reflection;
using System.Text;

namespace Exam.Filters
{
    public class LogActionAttribute : BaseAttribute
    {
        //public readonly IErrorService _errorService;

        //public LogActionAttribute(IErrorService errorService)
        //{
        //    _errorService = errorService;
        //}

        /// <summary>
        /// 记录控制器日志 API行为日志
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            var action = context.ActionDescriptor as ControllerActionDescriptor;
            var attr = action?.MethodInfo.GetCustomAttribute<ActionAttribute>();//获取日志特性
            if (attr != null)
            {
                string group = attr.Group;
                string name = attr.Action;
                string verstion = attr.Version;
                string url = context.HttpContext.Request.Path.ToString().ToUpper();
                string ip = this.GetIp();
                string paras = null;
                if (context.HttpContext.Request.Method.ToLower() == "post")
                {
                    var request = context.HttpContext.Request;
                    request.EnableBuffering();
                    if (request.Method.ToLower().Equals("post"))
                    {
                        request.Body.Seek(0, SeekOrigin.Begin);
                        //request.Body.CopyTo(new MemoryStream());
                        using var reader = new StreamReader(request.Body, Encoding.UTF8);
                        paras = reader.ReadToEnd();
                        if (paras != null)
                        {
                            if (paras.Length > 999)
                            {
                                paras = "参数太长";
                            }
                        }
                    }
                }
                else
                {
                    var keys = context.HttpContext.Request.Query.Keys;
                    foreach (var item in keys)
                    {
                        paras += $"{item}={context.HttpContext.Request.Query[item]};";
                    }
                    if (paras != null)
                    {
                        if (paras.Length > 999)
                        {
                            paras = "参数太长";
                        }
                    }
                }
                var FileName = DateTime.Now.ToString("yyyyMMdd") + ".txt";
                System.IO.File.AppendAllText(@"E:\S4\日志\ActionLogs\" + FileName, $"方法【{name}】,版本【{verstion}】,组分【{group}】" +
                    $"地址【{url}】,参数【{paras}】,IP【{ip}】");

                //记录本地
                var dto = new ErrorDto();

                //var dto = new ErrorDto()
                //{
                //    action = name,
                //    group = group,
                //    ip = ip,
                //    message = "",
                //    paras = paras,
                //    url = url,
                //    result = 0,
                //    uid = null,
                //    version = verstion,

                //};

                if (((ObjectResult)context.Result)?.Value is ApiResult result)
                {
                    dto.result = (int)result.code;
                    dto.message = result.message;
                    System.IO.File.AppendAllText(@"E:\S4\日志\ActionLogs\" + FileName, $"方法【{name}】,版本【{verstion}】,组分【{group}】" +
                        $"地址【{url}】,参数【{paras}】,IP【{ip}】,返回值【{dto.result}】,消息【{dto.message}】");
                   
                }
                //_errorService.AddActionLog(dto);
                //记录 
            }

            //不记录
        }
    }
}
